package com.chenfan.mcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.dao.CompanyTargetMapper;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.CompanyTarget;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.dto.CompanyTargetDTO;
import com.chenfan.mcn.model.dto.CompanyTargetSearchDTO;
import com.chenfan.mcn.model.vo.CompanyTargetPageVO;
import com.chenfan.mcn.service.CompanyTargetService;
import com.chenfan.mcn.utils.BusinessAssert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lizhejin
 * @since 2021-04-19
 */
@Service
public class CompanyTargetServiceImpl extends ServiceImpl<CompanyTargetMapper, CompanyTarget> implements CompanyTargetService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addTarget(CompanyTargetDTO companyTargetDTO) {
        verifyTargetCount(companyTargetDTO);
        CompanyTarget companyTarget = new CompanyTarget();
        companyTarget.setQuarterCode(companyTargetDTO.getQuarterCode());
        companyTarget.setTargetAmount(companyTargetDTO.getTargetAmount());
        companyTarget.setTargetCompanyId(4L);
        return baseMapper.insert(companyTarget);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateTarget(CompanyTargetDTO companyTargetDTO) {
        verifyTargetCount(companyTargetDTO);
        CompanyTarget companyTarget = new CompanyTarget();
        companyTarget.setId(companyTargetDTO.getId());
        companyTarget.setTargetAmount(companyTargetDTO.getTargetAmount());
        return baseMapper.updateById(companyTarget);
    }

    @Override
    public CompanyTargetDTO tailTarget(Long id) {
        CompanyTarget companyTarget = baseMapper.selectById(id);
        CompanyTargetDTO companyTargetDTO = new CompanyTargetDTO();
        companyTargetDTO.setId(companyTarget.getId());
        companyTargetDTO.setQuarterCode(companyTarget.getQuarterCode());
        companyTargetDTO.setTargetAmount(companyTarget.getTargetAmount());
        return companyTargetDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteTarget(Long id) {
        CompanyTarget entity = baseMapper.selectById(id);
        BusinessAssert.notNull(entity, McnErrorCode.COMPANY_TARGET_NOT_EXIST);
        CompanyTarget deleteCompanyTarget = new CompanyTarget();
        deleteCompanyTarget.setId(entity.getId());
        deleteCompanyTarget.setIsDelete(1);
        return baseMapper.updateById(deleteCompanyTarget);
    }

    @Override
    public Page<CompanyTargetPageVO> getPage(McnPage<CompanyTargetPageVO> mcnPage, CompanyTargetSearchDTO dto) {
        dto.setAlias("c.");
        dto.setTargetCompanyId(4L);
        return baseMapper.getPage(mcnPage, dto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateQuarterSales() {
        /** 更新公司目标的季度销售金额 
         * 取收入合同的创建时间在该季度时间范围内， 
         * 单据状态为【审批通过、审批中】状态的所有收入合同的【商务确认合同金额】总和，作为季度销售额保存落库
         */
        baseMapper.updateQuarterSales();

        /**
         * 更新目标达成率=季度销售额/目标金额
         */
        baseMapper.updateTargetReachRate();
    }

    /**
     * 校验目标是否已制定
     */
    private void verifyTargetCount(CompanyTargetDTO companyTargetDTO) {
        LambdaQueryWrapper<CompanyTarget> queryWrapper = Wrappers.lambdaQuery(CompanyTarget.class)
                .eq(CompanyTarget::getQuarterCode, companyTargetDTO.getQuarterCode())
                .eq(CompanyTarget::getIsDelete, Boolean.FALSE)
                .eq(CompanyTarget::getTargetCompanyId, 4);
        if (companyTargetDTO.getId() != null) {
            queryWrapper.ne(CompanyTarget::getId, companyTargetDTO.getId());
        }
        Integer existCount = baseMapper.selectCount(queryWrapper);
        if (existCount > 0) {
            throw new BusinessException(McnErrorCode.COMPANY_TARGET_EXIST);
        }
    }
}
